#!/usr/bin/env python
# Generate password hashes.
from crypt import crypt
from getpass import getpass
from optparse import OptionParser, make_option
from random import sample

def salt(gamut = './abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): 
    '''Generate a random salt from 'gamut'.'''
    return '$1$' + ''.join( sample(gamut, 8) ) + '$'

def getopts(): 
    '''Get 'options'.'''
    option_list = [
	make_option('-n', '--number', default = 1, type = 'int', 
	    help = 'Number of hashes to generate.'
	),
	make_option('-s', '--salt', default = '', type = 'string', 
	    help = 'Single-quote encapsulated salt to generate hash with.'
	)
    ]

    options, args = OptionParser(description = 'Generate hash(es) from a secret using randomly sampled salts.\n\nWithout input, hash \'hello\'.', 
	option_list = option_list, 
	usage = '%prog [-n 2]', 
	version = '%prog 0.1'
    ).parse_args()

    return (options, args)

def main(options, args): 
    '''Take options and generate hashes accordingly.'''
    # 'secret' is NEVER stored
    secret = getpass('Secret: ') or 'hello'
    for each in range(options.number): 
	print crypt(secret, options.salt or salt())
    
    return None

if __name__ == '__main__': 
    options, args = getopts()
    main(options, args)
